package viewer;

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class JRoffInit extends JApplet implements ActionListener {

    private JRoff mainFrame;
    private JButton startButton;
    private URL[] offFiles;

    public JRoffInit() {
        mainFrame = null;
        offFiles = null;
        startButton = new JButton("Start RoffView");
        startButton.setActionCommand("DISPLAY");
        startButton.addActionListener(this);
        this.getContentPane().add(startButton, BorderLayout.CENTER);
    }

    public void init() {

        ArrayList fileList = new ArrayList();
        BufferedReader bufread;
        String line;
        boolean failed = false;
        URL codebase = getCodeBase();
        URL badFile = null;
        URL u = null;
        offFiles = null;

        String fileparam = getParameter("offlist");
        if (fileparam == null)
            fileparam = "offlist.txt";
        try {
            u = new URL(codebase, fileparam);        
            badFile = new URL(codebase, "BadFile");
            bufread = new BufferedReader(new InputStreamReader(u.openStream()));
            line = bufread.readLine();
            while (line != null) {
                fileList.add(line.trim());
                line = bufread.readLine();
            }
            bufread.close();
        } catch (java.io.IOException e) {
            System.err.println("An error occured while loading the off file list from " + u.toString());
            failed = true;
        }
        if (!failed) {
           int numFiles = fileList.size();
           offFiles = new URL[numFiles];
           for (int i = 0; i < numFiles; i++) {
               try {
                   offFiles[i] = new URL(codebase, (String)fileList.get(i));
               } catch (MalformedURLException mue) {
                   offFiles[i] = badFile;
                   System.err.println("The file " + (String)fileList.get(i) + " listed in " + fileparam + " is not a valid URL");
               }
           }
        }
    }

    public void start() {
    }

    public void stop() {
    }

    public void destroy() {
        mainFrame.exitHandler();
        mainFrame.dispose();
        mainFrame = null;
    }

    public static void main(String[] args) {

        JRoff frame = new JRoff(false);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("DISPLAY")) {
            if (mainFrame != null) {
                mainFrame.exitHandler();
                mainFrame.dispose();
                mainFrame = null;
            }
            mainFrame = new JRoff(true);
            mainFrame.setAppletURLList(offFiles);
            mainFrame.setVisible(true);
        }
    }

    public String getAppletInfo() {
        return "RoffView Version 3.1. A 3D File Viewer by Ryan Holmes. (Ryan dot Holmes <at> asu dot edu)";
    }

    public String[][] getParamInfo() {
        String rval[][] = {
	     {"offlist",   "url",     "Relative url of text file containing list of relative urls of OFF files. Defaults to 'offlist.txt'"}
         };
        return rval;
    }
}
